package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.PlaceOrdersBrowseGoodsBean;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;

/**
 * 订单获取监控选择店铺Adapter
 * Created by ShineF on 2017/9/21 0021.
 */

public class PlaceOrdersBrowseGoodsDetailsRecycleAdapter
        extends RecyclerView.Adapter<PlaceOrdersBrowseGoodsDetailsRecycleAdapter.PlaceOrdersBrowseGoodsDetailsHolder> {


    private Context mContext;
    private ArrayList<PlaceOrdersBrowseGoodsBean.ItemListBean> mList;
    private int mHeight;
    private OnItemClickListener mListener;
    private OnAddGoodsListener mAddListener;

    public PlaceOrdersBrowseGoodsDetailsRecycleAdapter(ArrayList<PlaceOrdersBrowseGoodsBean.ItemListBean> list, int height, Context context) {
        mContext = context;
        mList = list;
        mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public void setOnAddGoodsListener(OnAddGoodsListener listener) {
        this.mAddListener = listener;
    }


    @Override
    public PlaceOrdersBrowseGoodsDetailsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PlaceOrdersBrowseGoodsDetailsHolder(View.inflate(mContext, R.layout.adapter_place_orders_browse_goods, null));
    }

    @Override
    public void onBindViewHolder(final PlaceOrdersBrowseGoodsDetailsHolder holder, final int position) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mRlItem.getLayoutParams();
        params.height = mHeight / 5;
        final PlaceOrdersBrowseGoodsBean.ItemListBean dataBean = mList.get(position);
        if (!TextUtils.isEmpty(dataBean.getItemName())) {
            holder.mTvGoodsName.setText(dataBean.getItemName());
        }
        if (!TextUtils.isEmpty(dataBean.getSupplierName())) {
            holder.mTvShopName.setText(dataBean.getSupplierName());
        }
        ImageloaderUtils.displayRoundImg(dataBean.getItemUrl(), holder.mIvGoodsView);
        holder.mTvGoodsSale.setText("可售:" + dataBean.getInventoryQty() + dataBean.getPbQtyUnit());
        holder.mTvGoodsPrice.setText("¥:" + dataBean.getPbPrice());

        holder.mRlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.ItemClickListener(position, view, dataBean);
                }
            }
        });

        holder.mIvAddView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAddListener != null) {
                    mAddListener.addCLickListener(position, holder.mIvAddView, dataBean);
                }
            }
        });

    }

    public interface OnItemClickListener {
        void ItemClickListener(int position, View view, PlaceOrdersBrowseGoodsBean.ItemListBean dataBean);

    }

    public interface OnAddGoodsListener {

        void addCLickListener(int position, View view, PlaceOrdersBrowseGoodsBean.ItemListBean dataBean);
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class PlaceOrdersBrowseGoodsDetailsHolder extends RecyclerView.ViewHolder {

        TextView mTvShopName;
        TextView mTvGoodsName;
        TextView mTvGoodsPrice;
        TextView mTvGoodsSale;
        ImageView mIvAddView;
        ImageView mIvGoodsView;
        RelativeLayout mRlItem;


        PlaceOrdersBrowseGoodsDetailsHolder(View itemView) {
            super(itemView);
            mTvShopName = (TextView) itemView.findViewById(R.id.tv_shop_name);
            mTvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
            mTvGoodsPrice = (TextView) itemView.findViewById(R.id.tv_price);
            mTvGoodsSale = (TextView) itemView.findViewById(R.id.tv_goods_sale);
            mIvAddView = (ImageView) itemView.findViewById(R.id.iv_add_view);
            mRlItem = (RelativeLayout) itemView.findViewById(R.id.rl_place_item);
            mIvGoodsView = (ImageView) itemView.findViewById(R.id.iv_goods_pic);
        }
    }
}
